import { createRouter, createWebHistory } from 'vue-router';
import Layout from '@/layouts/index.vue';
import i18n from '@/locales';
const { global } = i18n;
export const constantRoutes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index.vue'),
    meta: {
      title: '登录',
    },
    hidden: true,
  },
  {
    path: '/401',
    name: '401',
    component: () => import('@/views/errorPage/401.vue'),
    hidden: true,
  },
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/errorPage/404.vue'),
    hidden: true,
  },
   // 商家入住
   {
       path: '/recruit',
       name: 'recruit',
       component: () => import('@/views/recruit/index.vue'),
       hidden: true,
       meta: {
         title: '商家申请入住'
       }
   },
   // 同意政策
    {
        path: '/safety-policy',
        component: () => import('@/views/recruit/safety-policy.vue'),
        hidden: true,
        meta: {
          title: '华信商家版隐私政策'
        }
    },
   // 商家申请入住审核不通过
     {
         path: '/recruitedit',
         component: () => import('@/views/recruit/edit.vue'),
         hidden: true,
         meta: {
           title: '商家申请入住资料修改'
         }
     } 
     
];

export const asyncRoutes = [
  {
    path: '/',
    component: Layout,
    redirect: '/index',
    name: 'Root',
    children: [
      {
        path: '/index',
        name: 'Index',
        component: () => import('../views/index/index.vue'),
        meta: {
          title: global.t('route.home'),
          icon: 'icon-home',
          affix: true,
          noKeepAlive: true 
        },
      },
    ],
  },
  
  {
    path: '/user',
    component: Layout,
    name: 'Comp',   
    meta: { title: global.t('route.user'), icon: 'icon-table-file' },
    children: [
      {
        path: '/emplolyee',
        name: 'Employee',
        component: () => import('@/views/employee/index.vue'),
        meta: {
          title: global.t('route.employee'),
          icon: 'icon-table-file',
          permissions: ['admin']
        }
      },
      {
        path: '/merchandiser',
        name: 'Merchandiser',
        component: () => import('@/views/merchandiser/index.vue'),
        meta: {
          title: global.t('route.merchandiser'),
          icon: 'icon-table-file',
          permissions: ['admin']
        }
      },
      {
        path: '/detail',
        name: 'Detail',
        component: () => import('@/views/merchandiser/detail.vue'),
        meta: {
          title: global.t('route.merchandiserdetail'),
          icon: 'icon-table-file',
          permissions: ['merchandiser']
        }
      }
    ],
  },
   {
     path: '/product',
     component: Layout,
     name: 'Product',   
     meta: { title: global.t('route.product'), icon: 'icon-table-file' },
     children: [
       {
         path: '/category',
         name: 'Category',
         component: () => import('@/views/category/index.vue'),
         meta: {
           title: global.t('route.productcategory'),
           icon: 'icon-table-file'
         }
       },
       {
         path: '/list',
         name: 'ProductList',
         component: () => import('@/views/product/index.vue'),
         meta: {
           title: global.t('route.productList'),
           icon: 'icon-table-file' 
         }
       },
       {
         path: '/product-edit/:id(\\d+)?',
         name: 'ProductEdit',
          hidden: true,
         component: () => import('@/views/product/edit.vue'),
         meta: {
           title: global.t('route.productEdit'),
           icon: 'icon-table-file' ,
           permissions: ['merchandiser']
         } 
       },
       {
         path: '/product-copy/:id(\\d+)?',
         name: 'ProductCopy',
          hidden: true,
         component: () => import('@/views/product/copy.vue'),
         meta: {
           title: global.t('route.productCopy'),
           icon: 'icon-table-file' ,
           permissions: ['merchandiser']
         } 
       }
     ],
   } ,
   {
     path: '/orders',
     component: Layout,
     name: 'Orders',   
     meta: { title: global.t('route.orders'), icon: 'icon-table-file' },
     children: [
       {
         path: '/orderlist',
         name: 'Orderlist',
         component: () => import('@/views/orders/index.vue'),
         meta: {
           title: global.t('route.orderlist'),
           icon: 'icon-table-file'
         }
       } 
     ],
   }
];

const router = createRouter({
  history: createWebHistory(),
  routes: constantRoutes,
});

// reset router
export function resetRouter() {
  router.getRoutes().forEach((route) => {
    const { name } = route;
    if (name) {
      router.hasRoute(name) && router.removeRoute(name);
    }
  });
}

export default router;
